import aiohttp
from core.config import settings

class HTTPClient:

    _session:aiohttp.ClientSession = None

    @classmethod
    async def get_session(cls) ->aiohttp.ClientSession:
        """获取或创建全局HTTP会话"""
        if cls._session is None or cls._session.closed:
            cls._session = aiohttp.ClientSession(
                connector=aiohttp.TCPConnector(limit=10), #连接池大小
                timeout=aiohttp.ClientTimeout(total=settings.OLLAMA_TIMEOUT)
            )
        return cls._session

    @classmethod
    async def close(cls):
        """关闭HTTP会话"""
        if cls._session and not cls._session.closed:
            await cls._session.close()